/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.mp.comment.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.yami.shop.mp.comment.model.WeixinKeywordresponse;
import com.yami.shop.mp.comment.service.WeixinKeywordresponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 微信关键词回复
 * @author lgh on 2019/01/18.
 */
@RestController
@RequestMapping("/weixin/keywordresponse")
public class WeixinKeywordresponseController {

    @Autowired
    private WeixinKeywordresponseService weixinKeywordresponseService;

	/**
	 * 整页获取关键词回复
	 */
    @GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('weixin:keywordresponse:page')")
	public ResponseEntity<List<WeixinKeywordresponse>> page(){
		List<WeixinKeywordresponse> list = weixinKeywordresponseService.listWeixinKeywordresponse();
		for (WeixinKeywordresponse weixinKeywordresponse : list) {
			if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
				weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
			}
		}
		return ResponseEntity.ok(list);
	}

	/**
	 * 保存
	 */
	@PostMapping
	@PreAuthorize("@pms.hasPermission('weixin:keywordresponse:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid WeixinKeywordresponse weixinKeywordresponse){
		if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
			weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
		}
		weixinKeywordresponseService.save(weixinKeywordresponse);
		weixinKeywordresponseService.removeWeixinKeywordresponseCache();
		return ResponseEntity.ok().build();
	}

	/**
	 * 修改
	 */
	@PutMapping
	@PreAuthorize("@pms.hasPermission('weixin:keywordresponse:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid WeixinKeywordresponse weixinKeywordresponse){
		if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
			weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
		}
		weixinKeywordresponseService.updateById(weixinKeywordresponse);
		weixinKeywordresponseService.removeWeixinKeywordresponseCache();
		return ResponseEntity.ok().build();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('weixin:keywordresponse:delete')")
	public ResponseEntity<Void> delete(@PathVariable Long id){
		weixinKeywordresponseService.removeById(id);
		weixinKeywordresponseService.removeWeixinKeywordresponseCache();
		return ResponseEntity.ok().build();
	}
}
